from flask import request


class LimitCustom:
    @staticmethod
    def get_ipaddr():
        xff = request.headers.get("X-Forwarded-For", "")
        client_ip = xff.split(",")[-1].strip()
        return client_ip or request.headers.get("X-Real-Ip") or request.remote_addr or '127.0.0.1'

    @staticmethod
    def __get_uid():
        from eAuth.utils.auth import verify_token
        jwt_token = request.headers.get("Authorization")
        if jwt_token:
            user = verify_token(jwt_token)
            if user:
                return user.id

    @classmethod
    def limit_policy(cls):
        try:
            uid = cls.__get_uid()
            if uid:
                return uid
        except:
            pass
        return cls.get_ipaddr()
